package cn.xhjava.crm.workbench.service.impl;

import cn.xhjava.crm.commons.SysContants;
import cn.xhjava.crm.commons.util.DateUtils;
import cn.xhjava.crm.commons.util.ExceptionUtil;
import cn.xhjava.crm.commons.util.UUIDUtils;
import cn.xhjava.crm.settings.domain.User;
import cn.xhjava.crm.workbench.domain.*;
import cn.xhjava.crm.workbench.mapper.CustomerMapper;
import cn.xhjava.crm.workbench.mapper.TranHistoryMapper;
import cn.xhjava.crm.workbench.mapper.TranMapper;
import cn.xhjava.crm.workbench.service.TranService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @author Xiahu
 * @create 2023/7/19 0019
 */
@Service
public class TranServiceImpl implements TranService {
    @Autowired
    private TranMapper tranMapper;

    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private TranHistoryMapper tranHistoryMapper;


    @Override
    public List<Tran> selectForPage(Map<String, Object> map) throws Exception {
        return tranMapper.selectForPage(map);
    }

    @Override
    public int selectCountForPage(Map<String, Object> map) throws Exception {
        return tranMapper.selectCountForPage(map);
    }

    @Override
    public int delete(String[] ids) throws Exception {
        int res = 1;
        for (String id : ids) {
            tranMapper.deleteByPrimaryKey(id);
        }
        return res;
    }

    @Override
    public void add(Map<String, Object> map) throws Exception {
        String customerName = (String) map.get("customerName");
        User user = (User) map.get(SysContants.SESSION_USER);
        //根据name精确查询客户
        Customer customer = customerMapper.selectCustomerByName(customerName);
        //如果客户不存在，则新建客户
        if (customer == null) {
            customer = new Customer();
            customer.setOwner(user.getId());
            customer.setName(customerName);
            customer.setId(UUIDUtils.getUUID());
            customer.setCreateTime(DateUtils.formateDateTime(new Date()));
            customer.setCreateBy(user.getId());
            customerMapper.insert(customer);
        }
        //保存创建的交易
        Tran tran = new Tran();
        tran.setStage((String) map.get("stage"));
        tran.setOwner((String) map.get("owner"));
        tran.setNextContactTime((String) map.get("nextContactTime"));
        tran.setName((String) map.get("name"));
        tran.setMoney((String) map.get("money"));
        tran.setId(UUIDUtils.getUUID());
        tran.setExpectedDate((String) map.get("expectedDate"));
        tran.setCustomerId(customer.getId());
        tran.setCreateTime(DateUtils.formateDateTime(new Date()));
        tran.setCreateBy(user.getId());
        tran.setContactSummary((String) map.get("contactSummary"));
        tran.setContactsId((String) map.get("contactsId"));
        tran.setActivityId((String) map.get("activityId"));
        tran.setDescription((String) map.get("description"));
        tran.setSource((String) map.get("source"));
        tran.setType((String) map.get("type"));
        tranMapper.insert(tran);
        //保存交易历史
        TranHistory tranHistory = new TranHistory();
        tranHistory.setCreateBy(user.getId());
        tranHistory.setCreateTime(DateUtils.formateDateTime(new Date()));
        tranHistory.setExpectedDate(tran.getExpectedDate());
        tranHistory.setId(UUIDUtils.getUUID());
        tranHistory.setMoney(tran.getMoney());
        tranHistory.setStage(tran.getStage());
        tranHistory.setTranId(tran.getId());
        tranHistoryMapper.insert(tranHistory);
    }

    @Override
    public int update(Tran transaction) throws Exception {
        Customer customer = customerMapper.selectCustomerByName(transaction.getCustomerId());
        //如果客户不存在，则新建客户
        if (customer == null) {
            customer = new Customer();
            customer.setId(UUIDUtils.getUUID());
            customer.setOwner(transaction.getOwner());
            customer.setName(transaction.getCustomerId());
            customer.setCreateTime(DateUtils.formateDateTime(new Date()));
            customer.setCreateBy(transaction.getOwner());
            customerMapper.insert(customer);
            transaction.setContactsId(customer.getId());
        }

        //保存交易历史
        TranHistory tranHistory = new TranHistory();
        tranHistory.setCreateBy(transaction.getEditBy());
        tranHistory.setCreateTime(DateUtils.formateDateTime(new Date()));
        tranHistory.setExpectedDate(transaction.getExpectedDate());
        tranHistory.setId(UUIDUtils.getUUID());
        tranHistory.setMoney(transaction.getMoney());
        tranHistory.setStage(transaction.getStage());
        tranHistory.setTranId(transaction.getId());
        tranHistoryMapper.insert(tranHistory);

        return tranMapper.updateByPrimaryKeySelective(transaction);
    }

    @Override
    public Tran selectById(String id) throws Exception {
        return tranMapper.selectByPrimaryKey(id);
    }

    @Override
    public Tran selectDetailById(String id) throws Exception {
        return tranMapper.selectDetailById(id);
    }

    @Override
    public List<Tran> selectTransactionListByContactsId(String id) throws Exception {
        return tranMapper.selectTransactionListByContactsId(id);
    }

    @Override
    public int deleteById(String transactionId) throws Exception {
        return tranMapper.deleteByPrimaryKey(transactionId);
    }

    @Override
    public List<Tran> selectTransactionListByCustomerId(String id) throws Exception {
        return tranMapper.selectTransactionListByCustomerId(id);
    }

    @Override
    public List<FunnelVO> queryCountOfTranGroupByStage() throws Exception {
        return tranMapper.selectCountOfTranGroupByStage();
    }

    @Override
    public DateDataMapVO queryMoneyCountGroupByCreateTime() throws Exception {
        List<FunnelVO> funnelVOList = tranMapper.queryMonryCountGroupByCreateTime();
        DateDataMapVO res = new DateDataMapVO();
        LinkedList<String> date = new LinkedList();
        LinkedList<String> data = new LinkedList();
        for (FunnelVO vo : funnelVOList) {
            date.addLast(vo.getName());
            data.addLast(String.valueOf(vo.getValue()));
        }
        res.setDate(date);
        res.setData(data);
        return res;
    }
}
